using System;
using System.Collections.Generic;
using System.Text;

namespace NBusiness.CodeDom
{
    /// <summary>
    /// The Entity is a single conceptual member of a project, such as Employee, Address,
    /// LineItem, etc.
    /// </summary>
    /// <example>
    /// entity Employee
    /// {
    /// }
    /// </example>
	[Serializable]
	public abstract class Entity : EntityCompileUnit, IHasParent
	{
		#region Member properties and fields
		private string _name = null;
		public virtual string Name
		{
			get { return _name; }
			set { _name = value; }
		}

        public string Fullname
        {
            get
            {
                return (Parent != null ? (Parent as EntityFamily).Fullname + "." : "") + Name;
            }
        }
        public EntityRoot Root
        {
            get
            {
                IHasParent root = Parent;
                while (root.Parent != null) root = root.Parent;

                return root as EntityRoot;
            }
        }

		private bool _isReadOnly = false;
		public virtual bool IsReadOnly
		{
			get { return _isReadOnly; }
			set { _isReadOnly = value; }
		}

		private List<EntityField> _fields = new List<EntityField>();
		public virtual List<EntityField> Fields
		{
			get { return _fields; }
			set { _fields = value; }
		}

		private List<EntityRule> _rules = new List<EntityRule>();
		public virtual List<EntityRule> Rules
		{
			get { return _rules; }
			set { _rules = value; }
		}

		private List<EntityRelationship> _relationships = new List<EntityRelationship>();
		public virtual List<EntityRelationship> Relationships
		{
			get { return _relationships; }
			set { _relationships = value; }
		}

        private EntityFamily _parent = null;
        public virtual IHasParent Parent
        {
            get { return _parent; }
            set { _parent = (EntityFamily)value; }
        }

        private List<EntityTemplate> _as = new List<EntityTemplate>();
        public virtual List<EntityTemplate> As
        {
            get { return _as; }
        }

        private List<EntityAction> _actions = new List<EntityAction>();
        public virtual List<EntityAction> Actions
        {
            get { return _actions; }
        }
		#endregion

		#region object overrides
		public override string ToString()
		{
			return Name;
		}
		public override bool Equals(object obj)
		{
			if (!(obj is Entity)) return false;

			return Name.Equals((obj as Entity).Name);
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		} 
		#endregion
    }
}
